package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.Food;
import com.neusoft.elmboot.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/FoodController")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @RequestMapping("/listFoodByBusinessId")
    public List<Food> listFoodByBusinessId(Food food) throws Exception {
        return foodService.listFoodByBusinessId(food.getBusinessId());
    }

    @RequestMapping("/removeFoodByBusinessIdById")
    public Map<String, Object> removeFoodByBusinessIdById(@RequestBody Food food) throws Exception {
        Map<String, Object> response = new HashMap<>();
        
        int result = foodService.removeFoodByBusinessIdById(food.getFoodId(), food.getBusinessId());
        
        if (result == 1) {
            response.put("result", 1); // 删除成功
        } else {
            response.put("result", 0); // 删除失败
            response.put("message", "删除商品失败！");
        }
        
        return response;
    }

    @RequestMapping("/updateFood")
    public Map<String, Object> updateFood(@RequestBody Food food) throws Exception {
        Map<String, Object> response = new HashMap<>();

        // 检查是否传递了 foodId
        if (food.getFoodId() == null) {
            throw new RuntimeException("Food ID cannot be null");
        }

        // 调用服务层更新操作
        int result = foodService.updateFood(food);

        // 根据更新结果设置响应
        if (result == 1) {
            response.put("result", 1); // 更新成功
        } else {
            response.put("result", 0); // 更新失败
            response.put("message", "Failed to update food information.");
        }

        return response;
    }
    
    @RequestMapping("/addFood")
    public Map<String, Object> addFood(@RequestBody Food food) throws Exception {
        Map<String, Object> response = new HashMap<>();
        
        // 调用服务层添加操作
        int result = foodService.addFood(food);
        
        // 根据添加结果设置响应
        if (result > 0) {
            response.put("result", 1); // 添加成功
            response.put("foodId", result); // 返回新添加的食品ID
        } else {
            response.put("result", 0); // 添加失败
            response.put("message", "添加商品失败！");
        }
        
        return response;
    }
}
